﻿using System.Web;
using Grishko.SerilizeTools;
using Grishko.DataTier.SqlSentence;
using OutdoorLib.BaseEntity.Face;
using OutdoorLib.BaseEntity.Infrastructure;
using OutdoorLib.BaseEntity.Constraction;
using OutdoorLib.BaseEntity.Dictionary;
using OutdoorLib.BaseEntity.WS;
using System;
using Grishko.DataTier;
using System.Collections.Generic;
using System.Data;

namespace OutdoorLib.BaseEntity.Filters
{
    /// <summary>
    /// Базовый класс - для различного рода списка сторон
    /// </summary>
    [TagNameInfo(TagName = "SideListFilter")]
    public class SideListFilter : Filter
    {
        
        public class SideRow : Side
        {
            [SrchItemInfo(TxtPropertyName = "Конструкция")]
            public ConstractionEntity Constraction { get; set; }

            [SrchItemInfo(TxtPropertyName = "Группа")]
            public SurfaceGroup SurfaceGroup { get; set; }
            
            public Side SideEntity  
            {
                get {return (Side) Constraction.SideList[this.ID]; }
            }
        }

        public class SideListEngine : ScriptEngine
        {
            public SideListEngine(ScriptEngine.TScriptEngineSrchInfo Settings) : base(Settings) { }
            /// <summary>
            /// Дополнительно тянем описание конструкций
            /// </summary>
            /// <param name="Writer"></param>
            protected override void Script_GetObjects_6(System.Text.StringBuilder Writer)
            {
                string FormatString =
                              "select 	Constraction.id as CostractionID,Constraction.ObjectXml as ConstractionXML, " + Environment.NewLine +
                              "	Side.id as SideID, Side.ObjectXml  as SideXML from @Page_SelectedObjectId  Page Join    vSrchObjectPropertyStorage as Source on  Source.propertyvalue=Page .ObjectId " + Environment.NewLine +
                              "	join vSrchStorageLinks Constraction  on Constraction .id= Source .objectid " + Environment.NewLine +
                              "	join vSrchStorageLinks Side  on Side.id= Page  .objectid where Source.PropertyName='SideList' " + Environment.NewLine;
                Writer.Append(FormatString);
            }
        }

        public SideListFilter()
        {
            ScriptEngine.TScriptEngineSrchInfo Settings = new ScriptEngine.TScriptEngineSrchInfo();
            Settings.EntityType = typeof(SideRow);
            Settings.SqlFunction = "GetSideObjectPropertyStorage()";
            //Settings.PropertyCollection.Add("Constraction/Конструкция", typeof(OutdoorLib.BaseEntity.Constraction.ConstractionEntity));
            //Settings.BanProperties = new string[] { "Constraction.SideList" };
            Where = new SideListEngine(Settings);
        }

        public override List<EntitySingle> GetCollection()
        {

            List<EntitySingle> result = new List<EntitySingle>();
            SecuretyContext SC = AppContext.GetSC();
            try
            {

                if (null == Where) return result;
                string sql = Where.GetScript();

                if (string.IsNullOrEmpty(sql))
                    return result;

                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandText = sql;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = SC.Connection;
                SC.Connection.Open();

                DataSet DS = new DataSet();
                System.Data.SqlClient.SqlDataAdapter SqlD = new System.Data.SqlClient.SqlDataAdapter();
                SqlD.SelectCommand = cmd;
                SqlD.Fill(DS);
                if (0 == DS.Tables.Count)
                    return result;
                foreach (DataRow rw in DS.Tables[0].Rows)
                {
                    SideRow r = new SideRow();
                    r.ReadXml(rw["SideXML"].ToString());
                    ConstractionEntity constr = new ConstractionEntity();
                    constr.ReadXml(rw["ConstractionXML"].ToString());
                    r.Constraction = constr;
                    result.Add(r);
                }

            }
            finally
            {
                SC.Connection.Close();
            }

            return result;
        }
    }
}